package com.ncs.cns.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ncs.cns.R;
import com.ncs.cns.framework.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class ImagePagerActivity extends Activity {
	private int index;
	private List<String> photoPaths = new ArrayList<String>();//添加照片的路径list
	private DisplayImageOptions options;
	private ViewPager pager;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private int currentImgIndex;
	private TextView imgPagerInx; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_pager);
		
		Bundle bundle = this.getIntent().getExtras();
		index = bundle.getInt("index");
		currentImgIndex = index;
		photoPaths = bundle.getStringArrayList("paths");
		imgPagerInx = (TextView) this.findViewById(R.id.imgPagerInx);
		imgPagerInx.setText(index+1+"/"+photoPaths.size());
		
		if(!bundle.getBoolean(Constant.IS_SHOW_REMOVE_IMG_BUTTON,true)){
			Button btn = (Button) this.findViewById(R.id.delImg);
			btn.setVisibility(View.INVISIBLE);
		}
		
		options = new DisplayImageOptions.Builder()
		.showImageForEmptyUri(R.drawable.ic_empty)
		.showImageOnFail(R.drawable.ic_error)
		.resetViewBeforeLoading(true)
		.cacheOnDisc(true)
		.imageScaleType(ImageScaleType.EXACTLY)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new FadeInBitmapDisplayer(300))
		.build();
		
		pager = (ViewPager) this.findViewById(R.id.pager);
		pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(new ImagePagerAdapter());
		pager.setOnPageChangeListener(new OnPageChangeListener() {			
			@Override
			public void onPageSelected(int inx) {
				currentImgIndex	= inx;		
				imgPagerInx.setText(inx+1+"/"+photoPaths.size());
			}			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		pager.setCurrentItem(index);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.image_pager, menu);
		return true;
	}
	
	class ImagePagerAdapter extends PagerAdapter{

		private LayoutInflater inflater;
		
		public ImagePagerAdapter() {
			inflater = getLayoutInflater();
		}
		@Override
		public int getCount() {
			return photoPaths.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object obj) {
			return view.equals(obj);
		}
		
		
		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
			ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
			final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
			String path ="";
			if(photoPaths.get(position).startsWith("http")){
				path = photoPaths.get(position);
			}
			else{
				path = Constant.FILE_PREFIX+photoPaths.get(position);
			}
			imageLoader.displayImage(path, imageView, options, new SimpleImageLoadingListener() {
				@Override
				public void onLoadingStarted(String imageUri, View view) {
					spinner.setVisibility(View.VISIBLE);
				}
				@Override
				public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
					String message = null;
					switch (failReason.getType()) {		
						case IO_ERROR:				
							message = "图片不存在";
							break;
						case DECODING_ERROR:		
							message = "解析图片错误";
							break;
						case NETWORK_DENIED:		
							message = "您的网络太差，请稍后再试";
							break;
						case OUT_OF_MEMORY:		   
							message = "您的手机内存不足";
							break;
						case UNKNOWN:				
							message = "未知错误";
							break;
					}
					Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

					spinner.setVisibility(View.GONE);
				}

				@Override
				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
					spinner.setVisibility(View.GONE);		// 不显示圆形进度条
				}
			});
			((ViewPager) view).addView(imageLayout, 0);		// 将图片增加到ViewPager
			
			return imageLayout;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView((View) object);
		}
	}
	
	public void delImg(View view){
		Intent intent = new Intent();
		Bundle b = new Bundle();
		b.putInt("delImgInx",currentImgIndex);
		intent.putExtras(b);
		setResult(RESULT_OK,intent);
		this.finish();
	}

}
